* ======================================
* -------- Summary Politicians ---------
* ======================================



* Compute politicians' experience
* ===============================
	
	use "$dta\ImportPoliticians.dta", clear
	keep if running_loc == 1
	keep idPers year running_loc
	reshape wide running_loc, i(idPers) j(year)
	egen timesrun2015 = rowtotal(running_loc2003-running_loc2015) ///
		if running_loc2015 == 1 // Number of times run 2015
	egen timesrun2019 = rowtotal(running_loc2007-running_loc2019) ///
		if running_loc2019 == 1 // Number of times run 2019
	drop running_loc*
	reshape long timesrun, i(idPers) j(year)
	drop if timesrun == .
	gen first = 1*(timesrun <= 1) 	// not run before
	keep idPers year first
	tempfile lcdfirst
	save `lcdfirst'
	
	

* Arrange sample
* ==============

	use "$dta\PanelPoliticians.dta", clear
	
	keep Polit_id year Polit_Bonus Polit_Win Polit_Rank
	rename Polit_id idPers
	rename Polit_Bonus bonus
	rename Polit_Win elected
	rename Polit_Rank rank
	
	* Import experience
	merge 1:1 idPers year using `lcdfirst', keep(master matched) nogen
	
	* Import background characteristics
	merge m:1 idPers using "$dta\ImportPers.dta", nogen ///
		 keep(master matched) keepusing(birthyear immigrant female)
		 
	* Import education		 
	merge 1:1 idPers year using "$dta\ImportEduc.dta", nogen ///
		keep(master matched)
	
	* Import income
	merge 1:1 idPers year using "$dta\ImportIncome.dta", nogen ///
		keep(master matched) keepusing(incomeUSD)
	replace incomeUSD = 0 if incomeUSD == . // Treat missing as zero
	replace incomeUSD = incomeUSD*1000 // undo reported in 1000s
	
	gen age = year - birthyear
	order idPers year age first bonus rank elected immigrant female ///
		educlvl educ hieduc income

		
	* List of (close) family members
	preserve
	use $dta\NetworkFam1.dta, clear
	collapse (first) idPers, by(idFam1) // count each idFamily once	keep idFam1
	keep idFam1
	rename idFam1 idPers
	gen Fam1 = 1
	tempfile networkfamily
	save `networkfamily'
	restore
	
	* Import networks
	merge m:1 idPers using  `networkfamily', nogen keep(master matched) ///
		keepusing(Fam1)
	merge m:1 idPers using $dta\NetworkWork1.dta, nogen keep(master matched) ///
		keepusing(Work1)
	merge m:1 idPers using $dta\NetworkImm1.dta, nogen keep(master matched) ///
		keepusing(Imm1)

	
	
* Summarize
* =========
	
	foreach net in Fam1 Imm1 Work1 {
	
	forvalues t = 2015(4)2019{
		
		preserve
		
		keep if `net' == 1 
		keep if year == `t'
		
		* N's
		gen one = 1
		qui sum one
		local `net'_N_`t' = string(r(N), "%9.0fc")
	
		* No-decimals format	
		foreach var in age educlvl rank income{
			qui sum `var' if year == `t'
			local `net'_`var'_`t' = string(r(mean), "%9.0fc")
		}
	
		*Decimals format
		foreach var in first bonus elected female immigrant hieduc{
			gen `var'100 = `var'*100
			qui sum `var'100 if  year == `t'
			local `net'_`var'_`t' = string(r(mean), "%9.2fc")
		}
		restore
	}
	}	


* Build table
* ===========

	local tabhead "\begin{tabular}{lcccccc} \hline\hline"
	local row1  " &  \multicolumn{3}{c}{2015}  &  \multicolumn{3}{c}{2019} \\  \cmidrule(lr){2-4} \cmidrule(lr){5-7}"
	local row2 " & $\enskip$ Family $\enskip$  & Co-workers & Immigrants & $\enskip$ Family $\enskip$ & Co-workers & Immigrants \\ \cmidrule(lr){2-2} \cmidrule(lr){3-3} \cmidrule(lr){4-4} \cmidrule(lr){5-5} \cmidrule(lr){6-6} \cmidrule(lr){7-7}"
	local row3 " \\ \multicolumn{7}{l}{\textit{Political attributes}} \\"
	local row4 "\hspace{0.5cm} First time (percent) & `Fam1_first_2015' & `Work1_first_2015' & `Imm1_first_2015' & `Fam1_first_2019' & `Work1_first_2019' & `Imm1_first_2019' \\"
	local row5 "\hspace{0.5cm} Party bonus (percent) & `Fam1_bonus_2015' & `Work1_bonus_2015' & `Imm1_bonus_2015' & `Fam1_bonus_2019' & `Work1_bonus_2019' & `Imm1_bonus_2019' \\"
	local row6 "\hspace{0.5cm} List rank (average) & `Fam1_rank_2015' & `Work1_rank_2015' & `Imm1_rank_2015' & `Fam1_rank_2019' & `Work1_rank_2019' & `Imm1_rank_2019' \\"
	local row7 "\hspace{0.5cm} Elected (percent) & `Fam1_elected_2015' & `Work1_elected_2015' & `Imm1_elected_2015' & `Fam1_elected_2019' & `Work1_elected_2019' & `Imm1_elected_2019'\\"
	local row8 " \\ \multicolumn{7}{l}{\textit{Personal characteristics}} \\"
	local row9 "\hspace{0.5cm} Age (average) & `Fam1_age_2015' & `Work1_age_2015' & `Imm1_age_2015' & `Fam1_age_2019' & `Work1_age_2019' & `Imm1_age_2019' \\"
	local row10 "\hspace{0.5cm} Female (percent) & `Fam1_female_2015' & `Work1_female_2015' & `Imm1_female_2015' & `Fam1_female_2019' & `Work1_female_2019' & `Imm1_female_2019' \\"
	local row11 "\hspace{0.5cm} Immigrant (percent) & `Fam1_immigrant_2015' & `Work1_immigrant_2015' & `Imm1_immigrant_2015' & `Fam1_immigrant_2019' & `Work1_immigrant_2019' & `Imm1_immigrant_2019' \\"
	local row12 "\hspace{0.5cm} Higher education (percent) & `Fam1_hieduc_2015' & `Work1_hieduc_2015' & `Imm1_hieduc_2015'  & `Fam1_hieduc_2019' & `Work1_hieduc_2019' & `Imm1_hieduc_2019'  \\"
	local row13 "\hspace{0.5cm} Income (average) & `Fam1_income_2015' &  `Work1_income_2015' & `Imm1_income_2015' & `Fam1_income_2019' &  `Work1_income_2019' & `Imm1_income_2019'  \\"
	local row14 "\\ N & `Fam1_N_2015' & `Work1_N_2015' & `Imm1_N_2015' & `Fam1_N_2019' & `Work1_N_2019' & `Imm1_N_2019'  \\"


* Write table
* ===========

	capture file close politstats
	file open politstats using "$tbls\SummaryPoliticians.tex", write replace
	file write politstats "`tabhead'" _n
	file write politstats "\\" _n

	file write politstats "`row1'" _n
	file write politstats "`row2'" _n
	file write politstats "`row3'" _n
	file write politstats "`row4'" _n
	file write politstats "`row5'" _n
	file write politstats "`row6'" _n
	file write politstats "`row7'" _n
	file write politstats "`row8'" _n
	file write politstats "`row9'" _n
	file write politstats "`row10'" _n
	file write politstats "`row11'" _n
	file write politstats "`row12'" _n
	file write politstats "`row13'" _n
	file write politstats "`row14'" _n
	file write politstats "`row15'" _n

	file write politstats "\hline \hline" _n

	file write politstats "\end{tabular}"
	file close politstats
